package com.eoe.wiki.activity;

import java.util.Timer;
import java.util.TimerTask;

import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.eoe.wiki.R;
import com.eoe.wiki.WikiApplication;
import com.eoe.wiki.utils.L;
import com.eoe.wiki.utils.WikiUtil;
import com.eoe.wiki.view.SliderEntity;
import com.eoe.wiki.view.SliderLayer;
import com.umeng.analytics.MobclickAgent;

public class MainActivity extends ActivityGroup {
	
	private static WikiApplication mWikiApplication;
	private MainActivity mMainActivity;
	private LocalActivityManager mActivityManager;
	private boolean mReadyExit;
	private ExitTask mExitTask;
	private Timer mExitTimer;
	
	private SliderLayer mSliderLayers;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mWikiApplication = WikiApplication.getApplication();
		mMainActivity = this;
		mWikiApplication.setMainActivity(mMainActivity);
		setContentView(R.layout.main);
		
		mActivityManager = getLocalActivityManager();
		mExitTimer = new Timer();
		
		mSliderLayers = (SliderLayer)findViewById(R.id.animation_layout);
		MobclickAgent.onEvent(this, "home", "enter");
		int screenWidth = WikiUtil.getScreenWidth(mMainActivity);
		ViewGroup layer1 = (ViewGroup)findViewById(R.id.animation_layout_one);
		layer1.setPadding(0, 0, WikiUtil.dip2px(mMainActivity, 20), 0);
		
		ViewGroup layer2 = (ViewGroup)findViewById(R.id.animation_layout_two);
		layer2.setPadding(0, 0, WikiUtil.dip2px(mMainActivity, 15), 0);
		
		ViewGroup layer3 = (ViewGroup)findViewById(R.id.animation_layout_three);
		layer2.setPadding(0, 0, 0, 0);
		
		mSliderLayers.addLayer(new SliderEntity(layer1, 0, screenWidth, 0));
		mSliderLayers.addLayer(new SliderEntity(layer2, 0, screenWidth-WikiUtil.dip2px(mMainActivity, 23), 0));
		mSliderLayers.addLayer(new SliderEntity(layer3, WikiUtil.dip2px(mMainActivity, -10), screenWidth-WikiUtil.dip2px(mMainActivity, 20), 0));
		
		Intent intent = new Intent(this,MainCategoryActivity.class);
		showView(0,intent);
	}

	private void showView(final int index,Intent intent){
		intent.putExtra(SliderActivity.KEY_SLIDER_INDEX, index);
		String id = String.valueOf(System.currentTimeMillis());
		View view = mActivityManager.startActivity(id, intent).getDecorView();
		ViewGroup currentView = mSliderLayers.getLayer(index);
		currentView.removeAllViews();
		currentView.addView(view);
		
		if(index==0) return;
		
		view.post(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				mSliderLayers.openSidebar(index);
			}
		});
	}
	
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		if(mSliderLayers.isAnimationing()){
			return true;
		}
		return super.dispatchTouchEvent(ev);
	}

	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		// TODO Auto-generated method stub
		L.e("MainActivity disptchKeyEcent:" + event.getKeyCode());
		int keyCode = event.getKeyCode();
		int keyAction = event.getAction();
		
		switch(keyCode){
		case KeyEvent.KEYCODE_BACK:
			if(keyAction==KeyEvent.ACTION_DOWN){
				int index = mSliderLayers.openingLayerIndex();
				if(index>0){
					mSliderLayers.closeSidebar(index);
				}else{
					if(!mReadyExit){
						mReadyExit = true;
						Toast.makeText(mMainActivity, R.string.tip_exit, Toast.LENGTH_SHORT).show();
						if(mExitTask==null){
							mExitTask.cancel();
						}
						mExitTask = new ExitTask();
						mExitTimer.schedule(mExitTask, 2000);
					}else{
						sendBroadcast(new Intent(BaseActivity.ACTION_EXIT));
						finish();
					}
				}
			return true;
			}
		}
		return super.dispatchKeyEvent(event);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public SliderLayer getSliderLayer(){
		return this.mSliderLayers;
	}
	class ExitTask extends TimerTask{
		@Override
		public void run() {
			// TODO Auto-generated method stub
			mReadyExit = false;
		}
	}
}
